package com.example.project3;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import com.example.project3.fragment.HomeFragment;
import com.example.project3.fragment.MineFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;
    private HomeFragment homeFragment;
    private MineFragment mineFragment;
    private Fragment currentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomNavigationView = findViewById(R.id.bottom_navigation);

        // 初始化Fragment
        homeFragment = new HomeFragment();
        mineFragment = new MineFragment();

        // 首次加载显示首页
        if (savedInstanceState == null) {
            switchFragment(homeFragment);
        }

        // 底部导航切换事件
        bottomNavigationView.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.nav_home) {
                switchFragment(homeFragment);
                return true;
            } else if (itemId == R.id.nav_mine) {
                switchFragment(mineFragment);
                return true;
            }
            return false;
        });
    }

    // 切换Fragment的方法
    private void switchFragment(Fragment targetFragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        // 如果当前Fragment不存在，则添加
        if (!targetFragment.isAdded()) {
            transaction.add(R.id.fragment_container, targetFragment);
        }

        // 隐藏当前显示的Fragment，显示目标Fragment
        if (currentFragment != null) {
            transaction.hide(currentFragment);
        }
        transaction.show(targetFragment);
        transaction.commit();

        // 更新当前Fragment引用
        currentFragment = targetFragment;
    }

    // 保存Fragment状态，解决旋转屏幕重叠问题
    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        // 保存当前显示的Fragment标签
        if (currentFragment != null) {
            getSupportFragmentManager().putFragment(outState, "currentFragment", currentFragment);
        }
    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        // 恢复Fragment状态
        Fragment fragment = getSupportFragmentManager().getFragment(savedInstanceState, "currentFragment");
        if (fragment != null) {
            currentFragment = fragment;
            // 重新显示Fragment
            getSupportFragmentManager().beginTransaction()
                    .show(currentFragment)
                    .commit();
        }
    }
}